package Engine;

import Telas.Principal;
import java.awt.Color;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Engine {

    public static void Command(String text, String param) {

        switch (text) {
            case "sair":
                System.exit(0);
                break;
            case "veros":
                Principal.command.append("\n" + System.getProperty("os.name"));
                break;
            case "tema -branco":
                Principal.command.setBackground(Color.white);
                Principal.command.setCaretColor(Color.black);
                Principal.command.setForeground(Color.black);
                break;
            case "tema -padrao":
                Principal.command.setBackground(Color.black);
                Principal.command.setCaretColor(Color.green);
                Principal.command.setForeground(Color.green);
                break;
            case "veresteip":
                Enumeration<NetworkInterface> interfaces;
                try {
                    interfaces = NetworkInterface.getNetworkInterfaces();
                    while (interfaces.hasMoreElements()) {
                        NetworkInterface current = interfaces.nextElement();
                        System.out.println(current);
                        if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
                            continue;
                        }
                        Enumeration<InetAddress> addresses = current.getInetAddresses();
                        while (addresses.hasMoreElements()) {
                            InetAddress current_addr = addresses.nextElement();
                            if (current_addr.isLoopbackAddress()) {
                                continue;
                            }
                            Principal.command.append("\n" + current_addr.getHostAddress());
                        }
                    }
                } catch (SocketException ex) {
                    Logger.getLogger(Engine.class.getName()).log(Level.SEVERE, null, ex);
                }

                break;
            case "limpar":
                Principal.command.setText(null);
                break;
            case "verip -?":                                
                String ipAddress = param;
                InetAddress inet;
                try {
                    inet = InetAddress.getByName(ipAddress);
                    Principal.command.append("\nRequisitando a " + ipAddress + " : ");
                    Principal.command.append(inet.isReachable(3000) ? "Respondendo" : "Sem resposta\n");
                } catch (UnknownHostException ex) {
                    Logger.getLogger(Engine.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(Engine.class.getName()).log(Level.SEVERE, null, ex);
                }
                break;

            default:
                Principal.command.append("\nComando inválido.");
                break;
        }
    }
}
